﻿using Inovout.Cloud.DataWarehouse.Models;
using Inovout.Repositories;
using NHibernate;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Cloud.DataWarehouse.Repositories
{
    public class DataCollectionRepository : DataObjectRepository<DataCollection>
    {
        public void SaveDataCollectionViewTemplate(DataCollectionViewTemplate dataCollectionViewTemplate)
        {
            using (ISession session = OpenSession())
            {
                base.Save(dataCollectionViewTemplate.DataCollection);
                session.Save(dataCollectionViewTemplate);
            }
        }

        public void Delete(DataCollection dataCollection) 
        {
            using (ISession session = OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    try
                    {
                        //foreach (Customer c in customers)
                        //    _session.SaveOrUpdate(c);
                        //_session.Flush();
                        //tx.Commit();
                        //1.先删除properties数据
                        
                        Iesi.Collections.Generic.ISet<DataCollectionProperty> properties = dataCollection.Properties;
                        List<DataCollectionProperty> list = new List<DataCollectionProperty>();
                        foreach (DataCollectionProperty dataCollectionProperty in properties)
                        {
                            list.Add(dataCollectionProperty);
                        }
                        foreach (DataCollectionProperty dataCollectionProperty in list)
                        {
                            properties.Remove(dataCollectionProperty);
                            dataCollectionProperty.DataCollection = null;
                            if (dataCollectionProperty is DataCollectionRelationProperty)
                            {
                                DataCollectionRelationProperty dataCollectionRelationProperty = dataCollectionProperty as DataCollectionRelationProperty;
                                Iesi.Collections.Generic.ISet<DataCollectionRelationCondition> conditions = dataCollectionRelationProperty.Conditions;
                                foreach(DataCollectionRelationCondition dataCollectionRelationCondition in conditions)
                                {
                                    dataCollectionRelationCondition.TargetDataCollectionProperty = null;
                                    dataCollectionRelationCondition.SourceDataCollectionProperty = null;
                                    session.Delete(dataCollectionRelationCondition);
                                }
                            }
                            session.Delete(dataCollectionProperty);
                        }
                        string sql = "Delete from DataWarehouse_DataCollectionViewTemplate Where DataCollectionId = (select dataCollectionid from DataWarehouse_DataCollection where Name='" + dataCollection.Name + "')";
                        session.CreateSQLQuery(sql).ExecuteUpdate();
                        
                        session.Delete(dataCollection);
                        session.Flush();
                        tx.Commit();
                    }
                    catch (HibernateException)
                    {
                        tx.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
